package com.future.hair.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.hair.server.config.jjwt.JwtHelper;
import com.future.hair.server.config.result.CommonResult;
import com.future.hair.server.entity.User;
import com.future.hair.server.mapper.UserMapper;
import com.future.hair.server.model.PageModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



/**
*
* @author future-RogueYBJ
* @since 2020-05-23
* @version v1.0
*/

@RestController
@RequestMapping("//hair/user")
@Api(tags = "User管理接口")

public class UserController{
    @Autowired
    private UserMapper mapper;

    @ApiOperation(value = "查询 User 列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult<List<User>> userList(@RequestBody PageModel data,@RequestHeader String token) {
        return CommonResult.data(mapper.selectPage(new Page<>(data.getSize() , data.getCurrent()), null).getRecords());
    }

    @ApiOperation(value = "查询当前 User")
    @RequestMapping(value = "/id", method = RequestMethod.POST)
    public CommonResult<User> userId(@RequestBody User data,@RequestHeader String token) {
        return CommonResult.data(mapper.selectById(JwtHelper.getByUserId(token)));
    }

    @ApiOperation(value = "增加 User")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<User> create(@RequestBody User data) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("mobile", data.getMobile()); //请输入排异条件
        User u = mapper.selectOne(wrapper);
        if (null==u){
            data.setCreateTime(null);
            data.setUpdataTime(null);
            Integer result = mapper.insert(data);
            if (result == 1){
                String jwtToken = jwtToken(data);
                return CommonResult.data(data,jwtToken);
            }else {
                return CommonResult.failed("创建失败");
            }
        }else {
            String jwtToken = jwtToken(u);
            return CommonResult.data(u,jwtToken);
        }
    }

    @ApiOperation(value = "更新 User")
    @RequestMapping(value = "/upData", method = RequestMethod.POST)
    public CommonResult<Integer> upData(@RequestBody User data, @RequestHeader String token) {
        data.setId(JwtHelper.getByUserId(token));
        data.setUserId(null);
        data.setMobile(null);
        data.setCreateTime(null);
        data.setUpdataTime(null);
        data.setOpenId(null);
        Integer result = mapper.updateById(data);
        if (result==1){
            return CommonResult.data(result);
        }else {
            return CommonResult.failed("更新失败");
        }
    }

    @ApiOperation(value = "删除 User")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult<Integer> delete(@RequestBody User data,@RequestHeader String token) {
        return CommonResult.failed("该功能暂未开放");
//        Integer result = mapper.deleteById(data.getId());
//        if (result==1){
//            return CommonResult.data(result);
//        }else {
//            return CommonResult.failed("删除失败");
//        }
    }

    public String jwtToken(User user){
        if (null==user.getUsername()){
            user.setUsername("");
        }
        if (null==user.getMobile()){
            user.setMobile("");
        }
        if (null==user.getOpenId()){
            user.setOpenId(0);
        }
        return JwtHelper.generateToken(user.getUsername(),user.getMobile(),user.getId(),user.getOpenId(),0);
    }
}